import hashlib
import time
import random


# 这文件是为/account/authenticate生成sign

def nonce(i2):
    string_buffer = []
    for i3 in range(i2):
        string_buffer.append(
            "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"[random.randint(0, 61)])
    string_buffer = ''.join(string_buffer)
    return string_buffer


def timestamp():
    time_ = int(time.time())
    return time_



nonce = nonce(16)
timestamp = timestamp()

info = {
    "name": "Google%20Pixel2XL",
    "nonce": "UgylLz0zntisBye1",
    "platform": "udid",
    "timestamp": "1699430995",
    "udid": "164051e9-f063-3797-ae5b-c23397460a1f"
}

# 因为java有排序 在python中需要sorted来调整,sorted是根据acill码点进行排序
resultTwo = "&".join([f"{key}={info[key]}" for key in sorted(info.keys())])

# 加密前的明文
str_sign = "https://api2.tophub.app/account/authenticate" + resultTwo + 'aA4@ud^926U(}^r9'

print(str_sign.strip(""))


obj = hashlib.md5()
byte = obj.update(str_sign.encode('utf-8'))
hex_string = obj.hexdigest()
print("这是转化为16进制： ", hex_string)

# 对二进制进行hash
hash_bytes = obj.digest()

# 将字节字符串转换为字符数组
hash_array = list(hash_bytes)


a = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]


def b(byte):
    i2 = int(byte) // 16
    # print(i2)
    sb = ''
    strArr = a
    sb += strArr[i2]
    sb += strArr[byte % 16]
    return sb


def c(bArr):
    stringBuffer = ""
    for byte in bArr:
        stringBuffer += b(byte)
    return stringBuffer


# sign值：
g_sign = c(hash_array)
print(g_sign)
